<?php
/**
 * Created by PhpStorm.
 * User: ls721
 * Date: 2016/10/27 0027
 * Time: 3:50
 */class Model_Response extends PhalApi_Model_NotORM {
    public function getResponse($response_id)
    {
        $response=$this->getORM();
        $response->select('Response.id','Doctor.Name','Response.Status','Response.CreateDate');
        if(count($response_id)==1){
            $response->where('Response.id',$response_id[0]);
        }else{
            for($i=0;$i<count($response_id);$i++){
                $response->or('Response.id',$response_id[$i]);
            }
        }

        return $response->fetchRows();

    }

    public function listResponse($doctor_id)
    {
        $response=$this->getORM();
        $response->select(
            'Response.id as ResponseId',
            'Event_id as EventId',
            'Doctor_id as DoctorId',
            'Event.Location as Location',
            'Event.Title as Title',
            'Response.Status',
            'Response.CreateDate'
        ) ->where('Doctor_id',$doctor_id);
         return $response->fetchRows();

    }
    public function getResponseSingle($response_id)
    {
        return $this->getORM()->select(
            'Response.id as ResponseId',
            'Event_id as EventId',
            'Doctor_id as DoctorId',
            'Doctor.Name as DoctorName',
            'Response.Status',
            'Response.CreateDate',
            'Response.ResponseTime'
        )
       ->where('Response.id',$response_id)
       ->fetch();

    }
    public function listDoctorOfResponse($event_id)
    {
//        return $this->getORM()
//            ->select(
//                'Response.Doctor_id as DoctorId ',
//                'Doctor.Name',
//                'Doctor.Depart',
//                'Doctor.Hospital',
//                'Doctor.PhoneNumber',
//                'Doctor.DepartName',
//                'Doctor.Lng',
//                'Doctor.Lat',
//                'Response.Status',
//                'Response.CreateDate'
//
//            )
//            ->where('Event_id', $event_id)
//            ->order('CreateDate DESC')
//
//
//            ->fetchRows();
        $sql = 'SELECT
  D.id as DoctorId ,
  D.Name,
  D.DepartName,
  D.Depart,
  D.Hospital,
  D.PhoneNumber,
  D.Lng,
  D.Lat,
  D.Status AS DoctorStatus,
  ED.Status,
  ED.CreateDate,
  ED.ResponseTime
FROM Doctor AS D
  LEFT JOIN (
    SELECT R.Status,R.Doctor_id,R.CreateDate,ResponseTime
    FROM Response AS R
      LEFT JOIN Event AS E ON E.id = R.Event_id
    WHERE E.id=:EID) AS ED ON ED.Doctor_id=D.id
    ORDER BY ResponseTime DESC,CreateDate DESC  ,id DESC ';
        $params = array(':EID' => $event_id);;
        return $this->getORM()
            ->queryAll($sql, $params);
    }


    protected function getTableName($id) {
        return 'Response';
    }
}